package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        // 1. 校验参数
        //不喜欢需要登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //校验文章id不能为空     使用注解校验
        Long articleId = dto.getArticleId();
//        if (articleId == null){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"文章不存在");
//        }
        //校验不喜欢方式 只能是0 或 1   使用注解校验
        int type = dto.getType().intValue();
//        if(type!=0 && type!=1){
//            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"操作方式错误");
//        }
        // 2. 根据当前登录用户id查询行为实体对象
        Integer loginId = user.getId();
        Integer equipmentId = dto.getEquipmentId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(loginId, equipmentId);
        // 3. 如果是不喜欢操作  判断是否已经点过不喜欢
        String entryId = apBehaviorEntry.getId();
        if (type == 0){
            Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
            ApUnlikesBehavior queryResult = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (queryResult != null){
                CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"不能重复操作");
            }
            // 4. 没有点过不喜欢则 向mongo不喜欢集合中 添加不喜欢数据
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setEntryId(entryId);
            apUnlikesBehavior.setArticleId(articleId);
            apUnlikesBehavior.setCreatedTime(new Date());
            mongoTemplate.insert(apUnlikesBehavior);
        }
        // 5. 如果是取消不喜欢操作  在mongo不喜欢集合中  删除对应不喜欢数据
        if (type ==1){
            Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
            DeleteResult removeResult = mongoTemplate.remove(query, ApUnlikesBehavior.class);
        }
        return ResponseResult.okResult();
    }
}
